package com.thesis.project.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.thesis.project.domain.Algorithm;
import com.thesis.project.domain.Datatype;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service for processing Algorithm
 * 
 */
@Service("algorithmService")
@Transactional
public class AlgorithmService {

	protected static Logger logger = Logger.getLogger("service");
	
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	
	/**
	 * Retrieves all persons
	 * 
	 * @return a list of persons
	 */
	public List<Algorithm> getAll(Integer datatypeID) {
		logger.debug("Retrieving all algorithms");
		
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Create a Hibernate query (HQL)
		Query query = session.createQuery("FROM  Algorithm WHERE datatype.id=" +datatypeID);
		
		// Retrieve all
		return  query.list();
	}
	
	public List<Algorithm> getAll() {
		logger.debug("Retrieving all algorithms");
		
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Create a Hibernate query (HQL)
		Query query = session.createQuery("FROM  Algorithm");
		
		// Retrieve all
		return  query.list();
	}
	
	/**
	 * Retrieves a single algorithm
	 */
	public Algorithm get( Integer id ) {
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Retrieve existing person first
		Algorithm algorithm = (Algorithm) session.get(Algorithm.class, id);
		
		return algorithm;
	}
	/**
	 * Adds a new algorithm
	 */
	public void add(Algorithm algorithm) {
		logger.debug("Adding new algorithm");
		
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Save
		session.save(algorithm);
	}
	
	/**
	 * Deletes an existing algorithm
	 * @param id the id of the existing algorithm
	 */
	public void delete(Integer id) {
		logger.debug("Deleting existing algorithm");
		
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Retrieve existing person first
		Algorithm algorithm	 = (Algorithm) session.get(Algorithm.class, id);
		
		// Delete 
		session.delete(algorithm);
	}
	
	/**
	  * Deletes all algorithms based on the datatype's id
	  */
	 public void deleteAll(Integer datatypeID) {
	  logger.debug("Deleting existing algorithms based on datatype's id");
	   
	  // Retrieve session from Hibernate
	  Session session = sessionFactory.getCurrentSession();
	   
	  // Create a Hibernate query (HQL)
	  Query query = session.createQuery("DELETE FROM Algorithm WHERE datatype.id=" +datatypeID);
	   
	  // Delete all
	  query.executeUpdate();
	 }
	
	/**
	 * Edits an existing person
	 */
	public void edit(Algorithm algorithm) {
		logger.debug("Editing existing algorithm");
		
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Retrieve existing algorithm via id
		Algorithm existingAlgorithm = (Algorithm) session.get(Algorithm.class, algorithm.getId());
		
		// Assign updated values to this algorithm
		existingAlgorithm.setDescription(algorithm.getDescription());
		existingAlgorithm.setName(algorithm.getName());
		existingAlgorithm.setClassName(algorithm.getClassName());
		existingAlgorithm.setDatatype(algorithm.getDatatype());


		// Save updates
		session.save(existingAlgorithm);
	}
}
